<?php
/**
 *
 * 用户关系表
 *
 * UserRelationship.class.php
 * Created by IntelliJ IDEA.
 *
 * @author: hjh <hjh@jf.com>
 *
 * Date: 2017/9/8
 * Time: 20:08
 */

namespace Center;

use Common\Bridge;
use Common\DataType;
use Common\DBModelBase;

class UserRelationship extends DBModelBase
{
    /**
     * 以下需要在 继承类中重新定义
     */
    //memcache相关
    protected static $MEMCACHE = Bridge::MEMCACHE_DEFAULT;
    protected static $MC_SINGLE_KEY = 'USER_RELATIONSHIP_USER_ID:%d'; //单个key
    protected static $MC_EXPIRE = 7200;

    //mysql db 相关
    protected static $DB_WRITE = Bridge::DB_USER_W;
    protected static $DB_READ = Bridge::DB_USER_R;

    //本地缓存
    protected static $_CACHE = [];

    //表名
    protected static $TABLE_NAME = 'UserRelationship';
    //主键id
    protected static $PRIMARY_ID = 'UserId';

    const FIELD_USER_ID = 'UserId';
    const FIELD_INVITE_USER_ID = 'InviteUserId';
    const FIELD_CREATE_DATETIME = 'CreateTime';
    const FIELD_UPDATE_DATETIME = 'UpdateTime';
    const FIELD_STATUS = 'Status';

    const STATUS_WAIT_PAY = 0;
    const STATUS_WAIT_BIND = 1;
    const STATUS_ALREADY_BIND = 2;

    //表结构
    protected static $_FIELDS = [
        self::FIELD_USER_ID => DataType::T_INTEGER_NONNEGATIVE,
        self::FIELD_INVITE_USER_ID => DataType::T_INTEGER_NONNEGATIVE,
        self::FIELD_CREATE_DATETIME => DataType::T_DATETIME,
        self::FIELD_UPDATE_DATETIME => DataType::T_DATETIME_NULLABLE,
        self::FIELD_STATUS => DataType::T_INTEGER_NONNEGATIVE,
    ];

    /**
     * 添加关系表
     *
     * @param $userId
     * @param $inviteUserId
     * @return array|bool
     */
    public static function add($userId, $inviteUserId)
    {
        $now = DataType::datetime();
        $info = [
            self::FIELD_USER_ID => $userId,
            self::FIELD_INVITE_USER_ID => $inviteUserId,
            self::FIELD_CREATE_DATETIME => $now,
            self::FIELD_STATUS => self::STATUS_WAIT_PAY
        ];

        $data = static::formatAddData($info, [
            self::FIELD_USER_ID => 1,
            self::FIELD_INVITE_USER_ID => 1,
            self::FIELD_CREATE_DATETIME => 1,
            self::FIELD_STATUS => 1
        ]);

        if (empty($data)) {
            debug_log('addUserRelationship failed, ' . json_encode($info, JSON_UNESCAPED_UNICODE), JF_LOG_ERROR);
            return false;
        }
        $pdo_w = static::getPdo(true);
        $ret = $pdo_w->insert(static::$TABLE_NAME, $data);
        if (empty($ret)) {
            debug_log('addUserRelationship failed, ' .json_encode($data, JSON_UNESCAPED_UNICODE), JF_LOG_ERROR);
            return false;
        }
        debug_log("addUserRelationship userId:{$userId}, inviteUserId:{$inviteUserId}");
        return $data;
    }

    /**
     * 更新
     *
     * @param $userId
     * @param $info
     * @return bool
     */
    public static function update($userId, $info)
    {
        $data = static::updateData($userId, $info, [
            self::FIELD_USER_ID => 1,
            self::FIELD_INVITE_USER_ID => 1,
            self::FIELD_CREATE_DATETIME => 1
        ]);
        if ($data === false) {
            debug_log('updateUserRelationship failed, ' . json_encode($data, JSON_UNESCAPED_UNICODE), JF_LOG_ERROR);
            return false;
        }
        debug_log('updateUserRelationship success');
        return true;
    }
}
